package com.eatrip.dao.implementation;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.eatrip.bean.EventoLocale;
import com.eatrip.dao.EventiLocaleDAO;


@Repository
@Transactional
public class EventiLocaleDAOImple implements EventiLocaleDAO {
	
	private HibernateTemplate hibernateTemplate;
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public EventoLocale getEventoLocale(int idEventoLocale) {
		List<EventoLocale> list = hibernateTemplate.find("FROM EventoLocale WHERE idEvento=?",idEventoLocale);
		if (list.size() == 0)
			return null;
		return list.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EventoLocale> getEventiLocaleByIdLocale(int idLocale,
			int start, int numRow) {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(EventoLocale.class);
			criteria.add(Restrictions.eq("idLocaleFk", idLocale));
			criteria.setFirstResult(start);
			criteria.setMaxResults(numRow);
		List<EventoLocale> list = criteria.list();
		if (list.size() == 0)
			return null;
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EventoLocale> getEventiLocaleByDataInizio(Date dataInizio,
			int start, int numRow) {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(EventoLocale.class);
			criteria.add(Restrictions.eq("dataInizio", dataInizio));
			criteria.setFirstResult(start);
			criteria.setMaxResults(numRow);
		List<EventoLocale> list = criteria.list();
		if (list.size() == 0)
			return null;
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EventoLocale> getAllEventiLocali(int start, int numRow) {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(EventoLocale.class);
			criteria.setFirstResult(start);
			criteria.setMaxResults(numRow);
		List<EventoLocale> list = criteria.list();
		if (list.size() == 0)
			return null;
		return list;
	}

	@Override
	public void insertEventoLocale(EventoLocale eventoLocale) {
		hibernateTemplate.save(eventoLocale);

	}

	@Override
	public void updateEventoLocale(EventoLocale eventoLocale) {
		hibernateTemplate.update(eventoLocale);

	}

	@Override
	public void deleteEventoLocale(EventoLocale eventoLocale) {
		hibernateTemplate.delete(eventoLocale);

	}

	@Override
	public void deleteEventiLocaleByIdLocale(int idLocale) {
		hibernateTemplate.getSessionFactory().getCurrentSession().delete("DELETE FROM EventoLocale WHERE idLocaleFk = ? ", idLocale);
	}

	@Override
	public void deleteAllEventiLocali() {
		Query q = hibernateTemplate.getSessionFactory().getCurrentSession().createQuery("DELETE FROM EventoLocale");
		int row = q.executeUpdate();
		if (row == 0)
			System.out.println("errore cancellazione di tutti gli eventi");
	}

}
